package com.ruoyi.web.controller.ring.app;

import cn.hutool.core.convert.Convert;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.ring.domain.RingMyInfo;
import com.ruoyi.ring.domain.dto.RingMyInfoDTO;
import com.ruoyi.ring.domain.vo.FirmwareInfoVO;
import com.ruoyi.ring.domain.vo.WarrantyStatusVO;
import com.ruoyi.ring.service.IRingMyInfoService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 我的戒指信息 app
 * 
 * @author liuwp
 * @date 2025-10-15
 */
@RestController
@RequestMapping("/ring/app/ring_my_info")
public class AppMyInfoController extends BaseController
{
    @Resource
    private IRingMyInfoService ringMyInfoService;

    /**
     * 获取戒指固件信息 app
     */
    @GetMapping(value = "/query-firmware-info")
    public AjaxResult queryFirmwareInfo(String deviceSn)
    {
        FirmwareInfoVO vo = new FirmwareInfoVO();
        RingMyInfo myInfo = ringMyInfoService.selectOneRingMyInfo(Builder.of(RingMyInfo::new).with(RingMyInfo::setDeviceSn, deviceSn).build());
        if(myInfo != null){
            vo = Convert.convert(FirmwareInfoVO.class,myInfo);
        }
        return AjaxResult.success(vo);
    }

    /**
     * 获取戒指保修状态 app
     */
    @GetMapping(value = "/query-warranty-status")
    public AjaxResult queryWarrantyStatus(String deviceSn)
    {
        WarrantyStatusVO vo = new WarrantyStatusVO();
        RingMyInfo myInfo = ringMyInfoService.selectOneRingMyInfo(Builder.of(RingMyInfo::new).with(RingMyInfo::setDeviceSn, deviceSn).build());
        if(myInfo != null){
            vo = Convert.convert(WarrantyStatusVO.class,myInfo);
            vo.setActivationDate(DateUtils.dateTimeChinese(myInfo.getActivationDate()));
            vo.setExpirationDate(DateUtils.dateTimeChinese(myInfo.getExpirationDate()));
        }
        return AjaxResult.success(vo);
    }

    /**
     * 新增戒指固件信息 app
     */
    @Log(title = "我的戒指信息", businessType = BusinessType.INSERT)
    @PostMapping("/add-my-info")
    public AjaxResult operationMyOtherSet(@RequestBody RingMyInfoDTO dto)
    {
        RingMyInfo ringMyInfo = Convert.convert(RingMyInfo.class,dto);
        RingMyInfo myInfo = ringMyInfoService.selectOneRingMyInfo(Builder.of(RingMyInfo::new).with(RingMyInfo::setDeviceSn, dto.getDeviceSn()).build());
        if(myInfo != null){
            ringMyInfo.setId(myInfo.getId());
            return toAjax(ringMyInfoService.updateRingMyInfo(ringMyInfo));
        }else {
            ringMyInfo.setCreateTime(new Date());
            ringMyInfo.setActivationDate(new Date());
            ringMyInfo.setExpirationDate(DateUtils.addYears(new Date(),1));
            return toAjax(ringMyInfoService.insertRingMyInfo(ringMyInfo));
        }
    }

//    /**
//     * 激活戒指  更新激活日期
//     */
//    @Log(title = "我的戒指信息", businessType = BusinessType.UPDATE)
//    @PostMapping("/activate")
//    public AjaxResult activate()
//    {
//        RingMyInfo myInfo = ringMyInfoService.selectOneRingMyInfo(Builder.of(RingMyInfo::new).with(RingMyInfo::setUserId, SecurityUtils.getUserId()).build());
//        myInfo.setActivationDate(new Date());
//        myInfo.setExpirationDate(DateUtils.addYears(new Date(),1));
//        return toAjax(ringMyInfoService.updateRingMyInfo(myInfo));
//    }

}
